package com.xingfly.repository;

import com.xingfly.model.Notification;
import com.xingfly.model.jpa.NotifyTypeAndCount;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by SuperS on 2016/12/7.
 */
public interface NotificationRepository extends JpaRepository<Notification, Long> {
    @Query("SELECT n FROM Notification n WHERE n.notifiedUser.id=:userId AND n.notificationType=:type")
    List<Notification> findNotificationsByUserIdAndNotifyType(@Param("userId") Long userId, @Param("type") Integer notifyType);

    @Query("SELECT new com.xingfly.model.jpa.NotifyTypeAndCount(n.notificationType,count(n)) from Notification n WHERE n.notifiedUser.id=:userId GROUP BY n.notificationType")
    List<NotifyTypeAndCount> findNotificationTypeAndCountByUserId(@Param("userId") Long userId);

    @Modifying
    @Query("DELETE FROM Notification n WHERE n.event.id=:eventId AND n.notifier.id=:notifierId AND n.notifiedUser.id=:notifiedId")
    void deleteByEventIdAndNotifierIdAndNotifiedId(@Param("eventId") Long eventId, @Param("notifierId") Long notifierId, @Param("notifiedId") Long notifiedId);
}
